<?


$r = "../";
require_once($r."_engine/functions.php");
require_once($r."_engine/c_account.php");

define("LOGIN_INCORRECT", 1);
define("LOGIN_NOT_ACTIVATED", 2);
define("LOGIN_ACCOUNT_EXIPIRED", 3);

$action = $_POST["action"];


if ($action == "login") {
	$account = new CAccount();
	if (!$account->LoadByEmail($f_login_email) || 
		$f_login_password != $account->data["password"]) 
	{
		$status = LOGIN_INCORRECT;
		echo "Login Failure";
	}
	else 
	{
		if (!$account->data["activated"])
		{
			$status = LOGIN_NOT_ACTIVATED;
		}
		else
		{
			if ($account->data["expire"] < gmt_time())
			{
				$status = LOGIN_ACCOUNT_EXIPIRED;
			}
			else 
			{
				$account->Update(array(
					"last_login"	=> $account->data["current_login"],
					"current_login"	=> gmt_time(),
				));

				$g_session->Set($account->id, $account->data["password"], isset($f_remember));
				
				echo "Login Succeeded";
				//generate_html_redirect(@$ref ? $ref : "dashboard.php");
			}
		}
	}
}

//echo "<hr>";
if ($action == "signup") {
	
	$a = new CAccount();
	$res = $a->CreateNew();
	
	if ($res==2) echo "Email already exists";
	else if ($res==1) echo "SigUp Succeeded";
	else echo "SignUp Failed";
	
	$a->LoadByEmail($f_email);
	$a->Activate( $a->data["activation_code"] );

	$a->Update(array(
		"last_login"	=> gmt_time(),
		"current_login"	=> gmt_time(),
	));

}


//echo "<br>OK";

?>